package x.zero.controller.sys;




import x.zero.annotation.SysLog;
import x.zero.pojo.entity.SysTest;
import x.zero.service.sys.SysTestService;
import x.zero.util.EasyPoiUtils;
import x.zero.util.CommonResult;
import x.zero.util.ValidatorUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**

 * 测试
 */
@Slf4j
@RestController
@RequestMapping("/sys/sysTest")
public class SysTestController {

    @Autowired
    private SysTestService SysTestService;

    @SysLog(MODULE = "sys", REMARK = "根据条件查询所有测试列表")
    @ApiOperation("根据条件查询所有测试列表")
    @GetMapping(value = "/list")
    @PreAuthorize("hasAuthority('sys:sysTest:read')")
    public Object getSysTestByPage(SysTest entity,
                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        try {
            return CommonResult.success(SysTestService.page(pageNum,pageSize,entity));
        } catch (Exception e) {
            log.error("根据条件查询所有测试列表：%s", e.getMessage(), e);
        }
        return CommonResult.failed();
    }

    @SysLog(MODULE = "sys", REMARK = "保存测试")
    @ApiOperation("保存测试")
    @PostMapping(value = "/create")
    @PreAuthorize("hasAuthority('sys:sysTest:create')")
    public Object saveSysTest(@RequestBody SysTest entity) {
        try {

            if (SysTestService.save(entity)) {
                return CommonResult.success();
            }
        } catch (Exception e) {
            log.error("保存测试：%s", e.getMessage(), e);
            return CommonResult.failed(e.getMessage());
        }
        return CommonResult.failed();
    }

    @SysLog(MODULE = "sys", REMARK = "更新测试")
    @ApiOperation("更新测试")
    @PostMapping(value = "/update/{ids}")
    @PreAuthorize("hasAuthority('sys:sysTest:update')")
    public Object updateSysTest(@RequestBody SysTest entity) {
        try {
            if (SysTestService.updateById(entity)) {
                return CommonResult.success();
            }
        } catch (Exception e) {
            log.error("更新测试：%s", e.getMessage(), e);
            return CommonResult.failed(e.getMessage());
        }
        return CommonResult.failed();
    }

    @SysLog(MODULE = "sys", REMARK = "删除测试")
    @ApiOperation("删除测试")
    @GetMapping(value = "/delete/{ids}")
    @PreAuthorize("hasAuthority('sys:sysTest:delete')")
    public Object deleteSysTest(@ApiParam("测试id") @PathVariable Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return CommonResult.paramFailed("测试id");
            }
            if (SysTestService.deleteById(id)) {
                return CommonResult.success();
            }
        } catch (Exception e) {
            log.error("删除测试：%s", e.getMessage(), e);
            return CommonResult.failed();
        }
        return CommonResult.failed();
    }

    @SysLog(MODULE = "sys", REMARK = "给测试分配测试")
    @ApiOperation("查询测试明细")
    @GetMapping(value = "/{id}")
    @PreAuthorize("hasAuthority('sys:sysTest:read')")
    public Object getSysTestById(@ApiParam("测试id") @PathVariable Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return CommonResult.paramFailed("测试id");
            }
            SysTest coupon = SysTestService.queryById(id);
            return CommonResult.success(coupon);
        } catch (Exception e) {
            log.error("查询测试明细：%s", e.getMessage(), e);
            return CommonResult.failed();
        }

    }

    @ApiOperation(value = "批量删除测试")
    @RequestMapping(value = "/delete/batch", method = RequestMethod.GET)
    @SysLog(MODULE = "sys", REMARK = "批量删除测试")
    @PreAuthorize("hasAuthority('sys:sysTest:delete')")
    public Object deleteBatch(@RequestParam("ids") List<Long> ids) {
        boolean count = SysTestService.deleteById(ids);
        if (count) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }


    @SysLog(MODULE = "sys", REMARK = "导出社区数据")
    @GetMapping("/exportExcel")
    public void export(HttpServletResponse response, SysTest entity) {
        // 模拟从数据库获取需要导出的数据
        List<SysTest> personList = SysTestService.list(entity);
        // 导出操作
        EasyPoiUtils.exportExcel(personList, "导出社区数据", "社区数据", SysTest.class, "导出社区数据.xls", response);

    }

    @SysLog(MODULE = "sys", REMARK = "导入社区数据")
    @PostMapping("/importExcel")
    public void importUsers(@RequestParam MultipartFile file) {
        List<SysTest> personList = EasyPoiUtils.importExcel(file, SysTest.class);
        SysTestService.insertBatch(personList);
    }
}


